import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class P03_Panel extends JPanel  implements MouseListener, MouseMotionListener {
	
	private PanelListener listener;
	
	//1920
	private int width=0;
	//1080
	private int height=0;
	
	//menu control
	private Image[] imageMenu = new Image[9];
	private boolean[] menu_presseed = new boolean[4];
	private boolean help_pressed = false;
	
	private Image[] imageList = new Image[28];
	
	public int man=0;
	
	private int position=0;
	private List<Scale_Position> scale_position= new ArrayList<Scale_Position>();
	
	private boolean moving=false;
	
	public P03_Panel(int width, int height){
		
		this.width=width;
		this.height=height;
		
		//menu control
		imageMenu[0]=new ImageIcon(getClass().getResource("images/m/h0.png")).getImage();
		imageMenu[1]=new ImageIcon(getClass().getResource("images/m/h1.png")).getImage();
		imageMenu[2]=new ImageIcon(getClass().getResource("images/m/f0.png")).getImage();
		imageMenu[3]=new ImageIcon(getClass().getResource("images/m/f1.png")).getImage();
		imageMenu[4]=new ImageIcon(getClass().getResource("images/m/b0.png")).getImage();
		imageMenu[5]=new ImageIcon(getClass().getResource("images/m/b1.png")).getImage();
		imageMenu[6]=new ImageIcon(getClass().getResource("images/m/q0.png")).getImage();
		imageMenu[7]=new ImageIcon(getClass().getResource("images/m/q1.png")).getImage();
		imageMenu[8]=new ImageIcon(getClass().getResource("images/m/help/03.png")).getImage();
		
		menu_presseed[0]=false;
		menu_presseed[1]=false;
		menu_presseed[2]=false;
		menu_presseed[3]=false;
		
		
		imageList[0]=new ImageIcon(getClass().getResource("images/3/background.png")).getImage();
		imageList[1]=new ImageIcon(getClass().getResource("images/3/man.png")).getImage();
		imageList[2]=new ImageIcon(getClass().getResource("images/3/woman.png")).getImage();
		imageList[3]=new ImageIcon(getClass().getResource("images/3/scale_man.png")).getImage();
		imageList[4]=new ImageIcon(getClass().getResource("images/3/scale_woman.png")).getImage();
		
		imageList[5]=new ImageIcon(getClass().getResource("images/3/00.png")).getImage();
		imageList[6]=new ImageIcon(getClass().getResource("images/3/01.png")).getImage();
		imageList[7]=new ImageIcon(getClass().getResource("images/3/02.png")).getImage();
		imageList[8]=new ImageIcon(getClass().getResource("images/3/03.png")).getImage();
		imageList[9]=new ImageIcon(getClass().getResource("images/3/04.png")).getImage();
		imageList[10]=new ImageIcon(getClass().getResource("images/3/05.png")).getImage();
		imageList[11]=new ImageIcon(getClass().getResource("images/3/06.png")).getImage();
		imageList[12]=new ImageIcon(getClass().getResource("images/3/07.png")).getImage();
		imageList[13]=new ImageIcon(getClass().getResource("images/3/08.png")).getImage();
		imageList[14]=new ImageIcon(getClass().getResource("images/3/09.png")).getImage();
		imageList[15]=new ImageIcon(getClass().getResource("images/3/10.png")).getImage();
		imageList[16]=new ImageIcon(getClass().getResource("images/3/11.png")).getImage();
		imageList[17]=new ImageIcon(getClass().getResource("images/3/12.png")).getImage();
		imageList[18]=new ImageIcon(getClass().getResource("images/3/13.png")).getImage();
		imageList[19]=new ImageIcon(getClass().getResource("images/3/14.png")).getImage();
		
		imageList[20]=new ImageIcon(getClass().getResource("images/3/m/00.png")).getImage();
		imageList[21]=new ImageIcon(getClass().getResource("images/3/m/01.png")).getImage();
		imageList[22]=new ImageIcon(getClass().getResource("images/3/m/02.png")).getImage();
		imageList[23]=new ImageIcon(getClass().getResource("images/3/m/03.png")).getImage();
		
		imageList[24]=new ImageIcon(getClass().getResource("images/3/w/00.png")).getImage();
		imageList[25]=new ImageIcon(getClass().getResource("images/3/w/01.png")).getImage();
		imageList[26]=new ImageIcon(getClass().getResource("images/3/w/02.png")).getImage();
		imageList[27]=new ImageIcon(getClass().getResource("images/3/w/03.png")).getImage();
		
		addMouseListener(this);
		addMouseMotionListener(this);
	  	Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		
		scale_position.add(new Scale_Position(270,294.18951328028));
		scale_position.add(new Scale_Position(294.18951328028,318.157944598228));
		scale_position.add(new Scale_Position(318.157944598228,342.095551946648));
		
		scale_position.add(new Scale_Position(342.095551946648,5.84892159853171));
		
		scale_position.add(new Scale_Position(5.84892159853171,29.9536077332736));
		scale_position.add(new Scale_Position(29.9536077332736,53.9111058868552));
		scale_position.add(new Scale_Position(53.9111058868552,77.924711027526));
		scale_position.add(new Scale_Position(77.924711027526,101.802434534349));
		scale_position.add(new Scale_Position(101.802434534349,125.862849142143));
		scale_position.add(new Scale_Position(125.862849142143,149.803979392357));
		scale_position.add(new Scale_Position(149.803979392357,173.844831598807));
		scale_position.add(new Scale_Position(173.844831598807,197.904448053351));
		scale_position.add(new Scale_Position(197.904448053351,222.028837333099));
		scale_position.add(new Scale_Position(222.028837333099,246.065944650896));
		scale_position.add(new Scale_Position(246.065944650896,270));
		

	}
	  

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//draw background
		g.drawImage(imageList[0], 0, 0, null);
		
		//draw menu control

		//home
		if(menu_presseed[0]){
			g.drawImage(imageMenu[1], 1800, 20, null);
		}else{
			g.drawImage(imageMenu[0], 1800, 20, null);
		}
		
		
		//back
		
		if(menu_presseed[1]){
			g.drawImage(imageMenu[5], 20, 950, null);
		}else{
			g.drawImage(imageMenu[4], 20, 950, null);
		}
		
		
		//forward
		if(menu_presseed[2]){
			g.drawImage(imageMenu[3], 1830, 950, null);
		}else{
			g.drawImage(imageMenu[2], 1830, 950, null);
		}
		
		//help
		if(menu_presseed[3]){
			g.drawImage(imageMenu[7], 1732, 20, null);
		}else{
			g.drawImage(imageMenu[6], 1732, 20, null);
		}
		
		//help content
		if(help_pressed){
			g.drawImage(imageMenu[8], 1365, 79, null);
		}
				
		//scale
		//448 226
		g.drawImage(imageList[3+man],448,226, null);
		
		//human y:467
		//position : 0-14
		if(man==0){
			//man
			
			//0-1
			//2-5
			//6-8
			//9-
			//comment
			//244,274
			if(position<4){
				g.drawImage(imageList[20], 1200, 450, null);
			}else if(position<8){
				g.drawImage(imageList[21], 1200-5, 450, null);
			}else if(position<12){
				g.drawImage(imageList[22], 1200-5-7, 450, null);
			}else{
				g.drawImage(imageList[23], 1200-5-7-12, 450, null);
			}
			
		}else{
			//woman
			g.drawImage(imageList[2], 1200, 450, null);
			
			//0-2
			//3-5
			//6-10
			//11-
			
			if(position<4){
				g.drawImage(imageList[24], 1322, 170, null);
			}else if(position<8){
				g.drawImage(imageList[25], 1322, 170, null);
			}else if(position<12){
				g.drawImage(imageList[26], 1322, 170, null);
			}else{
				g.drawImage(imageList[27], 1322, 170, null);
			}		
		}
		
		//arc
		g.drawImage(imageList[5+position],500,380, null);
		
		//oval
		
		/*
		g.drawRect(500, 380, 410, 410);
		g.drawOval(500, 380, 410, 410);
		*/
		
		/*
		 * [center]
		 * 705,585 
		 */
		
		/*
		g.drawLine(705,585, 705, 380);
		g.drawLine(705,585, 789, 398);
		g.drawLine(705,585, 858, 448);
		g.drawLine(705,585, 900, 522);
		g.drawLine(705,585, 910, 606);
		g.drawLine(705,585, 882, 687);
		g.drawLine(705,585, 826, 751);
		g.drawLine(705,585, 748, 786);
		g.drawLine(705,585, 663, 786);
		g.drawLine(705,585, 585, 751);
		g.drawLine(705,585, 528, 688);
		g.drawLine(705,585, 501, 607);
		g.drawLine(705,585, 510, 522);
		g.drawLine(705,585, 553, 448);
		g.drawLine(705,585, 622, 398);
		*/
		
	}
	
	public void mousePressed(MouseEvent e) {
		
		//home
		if(1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			menu_presseed[0]=true;
		}
		
		//back
		
		if(20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[1]=true;
		}
		
		//forward
		if(1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[2]=true;
		}
		
		//help
		if(1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			menu_presseed[3]=true;
		}
		
		if(Math_N.distance(705, 585, e.getX(), e.getY())<=205){
			for(int i=0;i<scale_position.size();++i){
				if(i==3){
					if((scale_position.get(i).start<Math_N.angle(705,585,e.getX(),e.getY()) &&
							Math_N.angle(705,585,e.getX(),e.getY())<360)
							|| (0<Math_N.angle(705,585,e.getX(),e.getY()) &&
									Math_N.angle(705,585,e.getX(),e.getY())<scale_position.get(i).end)){
						position=i;
						break;
					}
				}else{
					if(scale_position.get(i).start<Math_N.angle(705,585,e.getX(),e.getY()) &&
							Math_N.angle(705,585,e.getX(),e.getY())<scale_position.get(i).end){
						position=i;
						break;
					}
				}
			}
		}
		
		
		
		this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
    	
    	if(menu_presseed[0] && 1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			//home
    		if(!moving){
    			listener.onHome();
        		moving=true;
    		}
		}else if(menu_presseed[1] && 20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//back
			if(!moving){
				listener.onBack();
				moving=true;
			}
		}else if(menu_presseed[2] && 1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//forward
			if(!moving){
				listener.onChangeValue(3, position);
				listener.onForward();
				moving=true;
			}
		}else if(menu_presseed[3] && 1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			help_pressed=!help_pressed;
		}
    	
    	menu_presseed[0]=false;
    	menu_presseed[1]=false;
    	menu_presseed[2]=false;
		menu_presseed[3]=false;
    	
    	this.repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
    	
    }
    
    public void mouseMoved(MouseEvent e) {
        
    	
    }
    
    public void mouseDragged(MouseEvent e) {
    	
    	if(Math_N.distance(705, 585, e.getX(), e.getY())<=205){
			for(int i=0;i<scale_position.size();++i){
				if(i==3){
					if((scale_position.get(i).start<Math_N.angle(705,585,e.getX(),e.getY()) &&
							Math_N.angle(705,585,e.getX(),e.getY())<360)
							|| (0<Math_N.angle(705,585,e.getX(),e.getY()) &&
									Math_N.angle(705,585,e.getX(),e.getY())<scale_position.get(i).end)){
						position=i;
						break;
					}
				}else{
					if(scale_position.get(i).start<Math_N.angle(705,585,e.getX(),e.getY()) &&
							Math_N.angle(705,585,e.getX(),e.getY())<scale_position.get(i).end){
						position=i;
						break;
					}
				}
			}
		}
    	this.repaint();
    }
    
    public void setPanelListener(PanelListener listener){
    	this.listener=listener;
    }
    
}